package com.domain.utils.wyyx;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Firrela
 * @time 2016/3/7.
 */
public class IMSet {

    private static Logger logger = LoggerFactory.getLogger(IMSet.class);

    private static String APPKEY = "f4969ff98d313177a195b652683c46ba";  //AppKey
    private static String SECRET = "02cd16703f58";  //AppSecret

    /**
     * 网易云信创建用户
     *
     * @param accid
     * @param name
     * @param icon   网易云通信ID头像URL
     * @param birth  用户生日，最大长度16字符
     * @param mobile 用户mobile，最大长度32字符
     * @param gender 用户性别，0表示未知，1表示男，2女表示女，其它会报参数错误
     * @param token
     * @return
     * @throws IOException
     */
    public static String createUser(String accid, String name, String icon, String birth, String mobile, String gender, String token) throws IOException {
        String url = "https://api.netease.im/nimserver/user/create.action";
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        /**
         * 网易云通信ID，最大长度32字符，必须保证一个
         APP内唯一（只允许字母、数字、半角下划线_、
         @、半角点以及半角-组成，不区分大小写， 会统一小写处理
         */
        params.add(new BasicNameValuePair("accid", accid));
        /**
         * 网易云通信ID昵称，最大长度64字符，用来PUSH推送
         时显示的昵称
         */
        params.add(new BasicNameValuePair("name", name));
        /**
         * 网易云通信ID头像URL，第三方可选填，最大长度1024
         */
        params.add(new BasicNameValuePair("icon", icon));

        //用户生日，最大长度16字符
        params.add(new BasicNameValuePair("birth", birth));

        //用户mobile，最大长度32字符
        params.add(new BasicNameValuePair("mobile", mobile));

        //用户性别，0表示未知，1表示男，2女表示女，其它会报参数错误
        params.add(new BasicNameValuePair("gender", gender));
        /**
         * 网易云通信ID可以指定登录token值，最大长度128字符，
         并更新，如果未指定，会自动生成token，并在
         创建成功后返回
         */
        params.add(new BasicNameValuePair("token", token));
        //UTF-8编码,解决中文问题
        HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
        String res = NIMPost.postNIMServer(url, entity, APPKEY, SECRET);
        logger.info("createUser httpRes: {}", res);
        return res;
    }

    /**
     * 更新用户信息
     *
     * @param accid
     * @param name
     * @param icon
     * @param birth
     * @param mobile
     * @param gender
     * @param
     * @return
     * @throws IOException
     */
    public static String updateUser(String accid, String name, String icon, String birth, String mobile, String gender) throws IOException {
        String url = "https://api.netease.im/nimserver/user/updateUinfo.action";
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("accid", accid));
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("icon", icon));
        params.add(new BasicNameValuePair("birth", birth));
        params.add(new BasicNameValuePair("mobile", mobile));
        params.add(new BasicNameValuePair("gender", gender));
        //UTF-8编码,解决中文问题
        HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
        String res = NIMPost.postNIMServer(url, entity, APPKEY, SECRET);
        logger.info("createUser httpRes: {}", res);
        return res;
    }

    /**
     * 设置桌面端在线时，移动端是否需要推送
     *
     * @param accid
     * @param donnopOpen true:移动端不需要推送，false:移动端需要推送
     * @return
     * @throws IOException
     */
    public static String setDonnop(String accid, String donnopOpen) throws IOException {
        String url = "https://api.netease.im/nimserver/user/setDonnop.action";
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("accid", accid));
        params.add(new BasicNameValuePair("donnopOpen", donnopOpen));
        //UTF-8编码,解决中文问题
        HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
        String res = NIMPost.postNIMServer(url, entity, APPKEY, SECRET);
        logger.info("createUser httpRes: {}", res);
        return res;
    }

    /**
     * 添加好友
     *
     * @param accid  加好友发起者accid
     * @param faccid 加好友接收者accid
     * @param type   1直接加好友，2请求加好友，3同意加好友，4拒绝加好友
     * @param msg    加好友对应的请求消息，第三方组装，最长256字符
     * @return
     * @throws IOException
     */
    public static String addFriend(String accid, String faccid, String type, String msg) throws IOException {
        String url = "https://api.netease.im/nimserver/friend/add.action";
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("accid", accid));
        params.add(new BasicNameValuePair("faccid", faccid));
        params.add(new BasicNameValuePair("type", type));
        params.add(new BasicNameValuePair("msg", msg));
        //UTF-8编码,解决中文问题
        HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
        String res = NIMPost.postNIMServer(url, entity, APPKEY, SECRET);
        logger.info("createUser httpRes: {}", res);
        return res;
    }

    /**
     * 删除好友
     *
     * @param accid  发起者accid
     * @param faccid 要删除朋友的accid
     * @return
     * @throws IOException
     */
    public static String deleteFriend(String accid, String faccid) throws IOException {
        String url = "https://api.netease.im/nimserver/friend/delete.action";
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("accid", accid));
        params.add(new BasicNameValuePair("faccid", faccid));
        //UTF-8编码,解决中文问题
        HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
        String res = NIMPost.postNIMServer(url, entity, APPKEY, SECRET);
        logger.info("createUser httpRes: {}", res);
        return res;
    }

    /**
     * 拉黑/取消拉黑；设置静音/取消静音
     *
     * @param accid
     * @param targetAcc    被加黑或加静音的帐号
     * @param relationType 本次操作的关系类型,1:黑名单操作，2:静音列表操作
     * @param value        操作值，0:取消黑名单或静音，1:加入黑名单或静音
     * @return
     * @throws IOException
     */
    public static String setSpecialRelation(String accid, String targetAcc, String relationType, String value) throws IOException {
        String url = "https://api.netease.im/nimserver/user/setSpecialRelation.action";
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("accid", accid));
        params.add(new BasicNameValuePair("targetAcc", targetAcc));
        params.add(new BasicNameValuePair("relationType", relationType));
        params.add(new BasicNameValuePair("value", value));
        //UTF-8编码,解决中文问题
        HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
        String res = NIMPost.postNIMServer(url, entity, APPKEY, SECRET);
        logger.info("createUser httpRes: {}", res);
        return res;
    }

    //撤回消息
    public static String recall(String deleteMsgid, String timetag) throws IOException {
        String url = "https://api.netease.im/nimserver/msg/recall.action";
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("deleteMsgid", deleteMsgid));
        params.add(new BasicNameValuePair("timetag", timetag));
        params.add(new BasicNameValuePair("type", "8"));
        params.add(new BasicNameValuePair("from", "d7ae70bcba9211e6834900163e001eaa"));
        params.add(new BasicNameValuePair("to", "23403704"));
        params.add(new BasicNameValuePair("msg", "消息撤回"));
        params.add(new BasicNameValuePair("ignoreTime", "1"));

        HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");

        String res = NIMPost.postNIMServer(url, entity, APPKEY, SECRET);
        logger.info("createUser httpRes: {}", res);
        return res;
    }


}
